import pandas as pd
import pickle
from configs.Config import Config
import warnings
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, classification_report, \
    confusion_matrix

warnings.filterwarnings("ignore")

conf = Config()

# 第一步：加载模型及向量化器
print("加载模型及向量化器")
with open(conf.random_forest_model_save_path, "rb") as f:
    model = pickle.load(f)
with open(conf.tfidf_model_save_path, "rb") as f:
    tfidf = pickle.load(f)
print("加载模型及向量化器完成")
# 第二步：读取dev数据
print("读取dev数据")
dev_df = pd.read_csv(conf.process_dev_datapath, sep='\t')
true_labels = dev_df["label"]
print("dev数据读取完成")
# 第三步：通过tfidf向量器，转换为数值特征
print("转换dev数据为数值...")
dev_features = tfidf.transform(dev_df['words'])
print("转换dev数据为数值完成")
# 第四步：进行模型预测与评估
print("进行预测...")
dev_predicts = model.predict(dev_features)
print("预测完成")
output_df = pd.DataFrame({"words": dev_df['words'], "predictions": dev_predicts, "true_labels": true_labels})
print("预测结果前5行：")
print(output_df.head(5))
# 模型评估
print("模型评估结果如下:")
print("准确率：", accuracy_score(true_labels, dev_predicts))
print("精确率：", precision_score(true_labels, dev_predicts, average='weighted'))
print("召回率：", recall_score(true_labels, dev_predicts, average='weighted'))
print("F1值：", f1_score(true_labels, dev_predicts, average='weighted'))
print("评估报告：", classification_report(true_labels, dev_predicts))  # 打印评估报告
print("混淆矩阵：", confusion_matrix(true_labels, dev_predicts))  # 打印混淆矩阵

# 第五步：保存预测结果
# 结果保存到result中
output_path = conf.model_predict_result
output_df.to_csv(output_path, index=False)
print(f"预测结果已保存到 {output_path}")
